package com.servicesystem.servicesystem.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.servicesystem.servicesystem.entity.UserAli;
import com.servicesystem.servicesystem.service.UserAliService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collections;

@Component
public class DefaultUserDetailsService implements UserDetailsService {

    @Autowired
    private UserAliService userAliService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<UserAli> userAliQueryWrapper = new QueryWrapper<>();
        userAliQueryWrapper.eq("username", username);
        UserAli userAli = userAliService.getOne(userAliQueryWrapper);
        if (userAli != null) {
            return new User(username, userAli.getPassword(), Collections.emptyList());
        } else {
            throw new UsernameNotFoundException("用户名不存在或者为空");
        }
    }
}
